package pl.edu.agh.agents.gui.actions;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import pl.edu.agh.agents.gui.GUI;

import com.mxgraph.util.mxPoint;
import com.mxgraph.view.mxGraph;

public class ZoomAction extends AbstractAction {
	private static final long serialVersionUID = 82674493280598658L;
	public static final String ZOOM_IN = " - ";
	public static final String ZOOM_OUT = " + ";
	private GUI gui;
	private String action;

	public ZoomAction(String name, GUI gui) {
		super(name);

		this.action = name;
		this.gui = gui;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		double factor = 1.;

		mxGraph graph = gui.getGraph();
		mxPoint translate = graph.getView().getTranslate();
		double oldX = translate.getX();
		double oldY = translate.getY();
		double oldScale = graph.getView().getScale();

		double scale = 1.;

		if (ZOOM_IN.equals(action)) {
			factor = 0.8;
			scale = factor * oldScale;
		}
		if (ZOOM_OUT.equals(action)) {
			factor = 1.25;
			scale = factor * oldScale;
		}

		graph.getModel().beginUpdate();

		graph.getView().scaleAndTranslate(scale, oldX * factor, oldY * factor);

		graph.getModel().endUpdate();
	}
}
